/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			editimpl.cc
  \date			Dec 2012
  \author		TNick

  \brief		Contains the implementation of EditImpl class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<cpg/cpg.h>

#include	<cpg/actions/actionshost.h>
#include	<cpg/actions/editorbase.h>
#include	"editimpl.h"
#include	<QMenu>
#include	<QClipboard>
#include	<QToolBar>


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */


/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
EditImpl::EditImpl		( QObject * parent )
	:QObject( parent ), MemTrack()
{
	host_ = NULL;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
EditImpl::~EditImpl	( void )
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
bool						EditImpl::init				( ActionsHost * host )
{

	host_ = host;
	ui_.setupUi( host->uiParentWidget() );

	/* connect the actions to local slots */
	connect(ui_.action_Undo,    SIGNAL(triggered()), &undogr_, SLOT(undo()));
	connect(ui_.action_Redo,    SIGNAL(triggered()), &undogr_, SLOT(redo()));
	connect(ui_.action_Cut,     SIGNAL(triggered()), this, SLOT(slot_Cut()));
	connect(ui_.action_Copy,    SIGNAL(triggered()), this, SLOT(slot_Copy()));
	connect(ui_.action_Paste,   SIGNAL(triggered()), this, SLOT(slot_Paste()));
	connect(ui_.action_Delete,  SIGNAL(triggered()), this, SLOT(slot_Delete()));
	connect(ui_.action_SelAll,  SIGNAL(triggered()), this, SLOT(slot_SelAll()));
	connect(ui_.action_Preferences,  SIGNAL(triggered()), this, SIGNAL(showPrefs()));
	connect(ui_.action_Find,  SIGNAL(triggered()), this, SLOT(showFind()));

	connect( &undogr_,				 SIGNAL(canUndoChanged(bool)),
			 ui_.action_Undo, SLOT(setEnabled(bool)));
	connect( &undogr_,				 SIGNAL(canRedoChanged(bool)),
			 ui_.action_Redo, SLOT(setEnabled(bool)));

	ui_.action_Undo->setEnabled( false );
	ui_.action_Redo->setEnabled( false );

	ui_.action_Undo->setWhatsThis( ui_.action_Undo->toolTip() );
	ui_.action_Redo->setWhatsThis( ui_.action_Redo->toolTip() );
	ui_.action_Cut->setWhatsThis( ui_.action_Cut->toolTip() );
	ui_.action_Copy->setWhatsThis( ui_.action_Copy->toolTip() );
	ui_.action_Paste->setWhatsThis( ui_.action_Paste->toolTip() );
	ui_.action_Delete->setWhatsThis( ui_.action_Delete->toolTip() );
	ui_.action_SelAll->setWhatsThis( ui_.action_SelAll->toolTip() );
	ui_.action_Find->setWhatsThis( ui_.action_Find->toolTip() );
	ui_.action_Preferences->setWhatsThis( ui_.action_Preferences->toolTip() );

	ui_.action_Undo->setStatusTip( ui_.action_Undo->toolTip() );
	ui_.action_Redo->setStatusTip( ui_.action_Redo->toolTip() );
	ui_.action_Cut->setStatusTip( ui_.action_Cut->toolTip() );
	ui_.action_Copy->setStatusTip( ui_.action_Copy->toolTip() );
	ui_.action_Paste->setStatusTip( ui_.action_Paste->toolTip() );
	ui_.action_Delete->setStatusTip( ui_.action_Delete->toolTip() );
	ui_.action_SelAll->setStatusTip( ui_.action_SelAll->toolTip() );
	ui_.action_Find->setStatusTip( ui_.action_Find->toolTip() );
	ui_.action_Preferences->setStatusTip( ui_.action_Preferences->toolTip() );

	ui_.action_Undo->setShortcuts( QKeySequence::keyBindings( QKeySequence::Undo ) );
	ui_.action_Redo->setShortcuts( QKeySequence::keyBindings( QKeySequence::Redo ) );
	ui_.action_Cut->setShortcuts( QKeySequence::keyBindings( QKeySequence::Cut ) );
	ui_.action_Copy->setShortcuts( QKeySequence::keyBindings( QKeySequence::Copy ) );
	ui_.action_Paste->setShortcuts( QKeySequence::keyBindings( QKeySequence::Paste ) );
	ui_.action_Delete->setShortcuts( QKeySequence::keyBindings( QKeySequence::Delete ) );
	ui_.action_SelAll->setShortcuts( QKeySequence::keyBindings( QKeySequence::SelectAll ) );
	ui_.action_Find->setShortcuts( QKeySequence::keyBindings( QKeySequence::Find ) );
	ui_.action_Preferences->setShortcuts( QKeySequence::keyBindings( QKeySequence::Preferences ) );

	activeEditorChanged( NULL );
	return true;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					EditImpl::end					( void )
{
	/* stub */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					EditImpl::initMenu				( QMenu * menu )
{
	menu->addAction( ui_.action_Undo );
	menu->addAction( ui_.action_Redo );
	menu->addSeparator();
	menu->addAction( ui_.action_Cut );
	menu->addAction( ui_.action_Copy );
	menu->addAction( ui_.action_Paste );
	menu->addAction( ui_.action_Delete );
	menu->addSeparator();
	menu->addAction( ui_.action_SelAll );
	menu->addSeparator();
	menu->addAction( ui_.action_Find );
	menu->addSeparator();
	menu->addAction( ui_.action_Preferences );

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					EditImpl::initCtxMenu			( QMenu * menu )
{

	menu->addAction( ui_.action_Cut );
	menu->addAction( ui_.action_Copy );
	menu->addAction( ui_.action_Paste );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					EditImpl::initToolbar			( QToolBar * tb )
{
	tb->addAction( ui_.action_Undo );
	tb->addAction( ui_.action_Redo );
	tb->addSeparator();
	tb->addAction( ui_.action_Cut );
	tb->addAction( ui_.action_Copy );
	tb->addAction( ui_.action_Paste );
	tb->addSeparator();
	tb->addAction( ui_.action_Find );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			EditImpl::editorCreated		( EditorBase * ed )
{
	undogr_.addStack( ed->undoStack() );
	connect( ed->asWidget(), SIGNAL( findVisibilityChanged(bool) ),
			 this, SLOT( findVisibilityChanged(bool) )
			 );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void						EditImpl::editorCursorMoved	(
		EditorBase* ed, bool b_with_sel )
{
	Q_UNUSED( ed );
	ui_.action_Cut->setEnabled( b_with_sel );
	ui_.action_Copy->setEnabled( b_with_sel );
	ui_.action_Delete->setEnabled( b_with_sel );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void					EditImpl::activeEditorChanged		( EditorBase * ed )
{

	bool with_file = ( ed != NULL );

	if ( with_file )
	{

		QUndoStack * stk = ed->undoStack();
		if ( undogr_.stacks().contains( stk ) == false )
		{
			undogr_.addStack( stk );
		}
		undogr_.setActiveStack( stk );

		bool b = ed->hasSelection();
		ui_.action_Cut->setEnabled( b );
		ui_.action_Copy->setEnabled( b );
		ui_.action_Paste->setEnabled( true );
		ui_.action_Delete->setEnabled( b );
		ui_.action_SelAll->setEnabled( true );
		ui_.action_Find->setEnabled( true );
		ui_.action_Find->setChecked( ed->isFindVisible() );

	}
	else
	{
		ui_.action_Cut->setEnabled( false );
		ui_.action_Copy->setEnabled( false );
		ui_.action_Paste->setEnabled( false );
		ui_.action_Delete->setEnabled( false );
		ui_.action_SelAll->setEnabled( false );
		ui_.action_Find->setEnabled( false );
		ui_.action_Find->setChecked( false );
	}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        			EditImpl::slot_Cut            	( void )
{
	PGDBG2( PGDBG_A_EDIT, "EditImpl: cut ", this );

	EditorBase * ed = host_->crtEditor();
	PGDBG2( PGDBG_A_EDIT, "  - document", ed );

	if ( ed == NULL )
	{
		return;
	}
	ed->cut();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        			EditImpl::slot_Copy            	( void )
{
	PGDBG2( PGDBG_A_EDIT, "EditImpl: copy ", this );

	EditorBase * ed = host_->crtEditor();
	PGDBG2( PGDBG_A_EDIT, "  - document", ed );

	if ( ed == NULL )
	{
		return;
	}
	ed->copy();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        			EditImpl::slot_Paste            	( void )
{
	PGDBG2( PGDBG_A_EDIT, "EditImpl: paste ", this );

	EditorBase * ed = host_->crtEditor();
	PGDBG2( PGDBG_A_EDIT, "  - document", ed );

	if ( ed == NULL )
	{
		return;
	}
	ed->paste();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        			EditImpl::slot_Delete            	( void )
{
	PGDBG2( PGDBG_A_EDIT, "EditImpl: delete ", this );

	EditorBase * ed = host_->crtEditor();
	PGDBG2( PGDBG_A_EDIT, "  - document", ed );

	if ( ed == NULL )
	{
		return;
	}
	ed->deleteSel();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        			EditImpl::slot_SelAll            	( void )
{
	PGDBG2( PGDBG_A_EDIT, "EditImpl: select all ", this );

	EditorBase * ed = host_->crtEditor();
	PGDBG2( PGDBG_A_EDIT, "  - document", ed );

	if ( ed == NULL )
	{
		return;
	}
	ed->selectAll();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void						EditImpl::editorClosing		( EditorBase * ed )
{
	undogr_.removeStack( ed->undoStack() );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void						EditImpl::showFind			( void )
{
	PGDBG2( PGDBG_A_EDIT, "EditImpl: show find ", this );

	EditorBase * ed = host_->crtEditor();
	PGDBG2( PGDBG_A_EDIT, "  - document", ed );

	ed->showFind( true );
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void						EditImpl::findVisibilityChanged	( bool b_on )
{
	PGDBG2( PGDBG_A_EDIT, "EditImpl: findVisibilityChanged ", this );
	PGDBG2( PGDBG_A_EDIT, "  - shoving", b_on );

	Q_UNUSED( b_on );
	EditorBase * ed = host_->crtEditor();
	PGDBG2( PGDBG_A_EDIT, "  - document", ed );
	if ( ed == NULL )
		return;

	if ( static_cast<QWidget*>( sender() ) == ed->asWidget() )
	{
		// ui_.action_Find->setChecked( b_on );
	}

}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */

